﻿using SqlSugar;
using System.Linq.Expressions;
using Zhao.Common.Helper;
using Zhao.IServer;
using Zhao.Models.Entity;
using Zhao.Repository.Base;
using Zhao.Repository.UnitOfWork;

namespace Zhao.Service
{
    public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new()
    {
        protected IBaseRepository<TEntity> _baseDal;

        protected ISqlSugarClient _Client { get; set; }

        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="baseDal"></param>
        public BaseService(IBaseRepository<TEntity> baseDal,IUnitOfWork unitOfWork)
        {
            _baseDal = baseDal;
            _Client = unitOfWork.GetDbClient();
        }


        #region 同步

        #region 查询

        public TEntity Query<T>(T primaryKey, bool blnUseCache = false)
        {
            return _baseDal.Query(primaryKey, blnUseCache);
        }

        public TEntity Query(Expression<Func<TEntity, bool>> where, bool blnUseCache = false)
        {
            return _baseDal.Query(where, blnUseCache);
        }

        public TEntity QueryOrder(Expression<Func<TEntity, bool>> expression, string strOrderByFileds = null, bool blnUseCache = false)
        {
            return _baseDal.QueryOrder(expression, strOrderByFileds, blnUseCache);
        }

        public List<TEntity> QueryList(Expression<Func<TEntity, bool>> expression, int top, string strOrderByFileds = null, bool blnUseCache = false)
        {
            return _baseDal.QueryList(expression, top, strOrderByFileds, blnUseCache);
        }

        public List<TEntity> QueryList(Expression<Func<TEntity, bool>> expression, string strOrderByFileds = null, bool blnUseCache = false)
        {
            return _baseDal.QueryList(expression, strOrderByFileds, blnUseCache);
        }

        public PageModel<TEntity> QueryPage(Expression<Func<TEntity, bool>> where, string order, int intPageIndex = 1, int intPageSize = 20)
        {
            return _baseDal.QueryPage(where, order, intPageIndex, intPageSize);
        }

        public int QueryCount(Expression<Func<TEntity, bool>> whereExpression)
        {
            return _baseDal.QueryCount(whereExpression);
        }
        #endregion

        #region 添加

        public int Add(TEntity entity)
        {
            return _baseDal.Add(entity);
        }

        public int Add(List<TEntity> entities)
        {
            return _baseDal.Add(entities);
        }

        #endregion

        #region 修改
        public bool Update(TEntity entity)
        {
            return _baseDal.Update(entity);
        }

        public bool Update(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> where)
        {
            return _baseDal.Update(columns, where);
        }

        public int Update(List<TEntity> entities)
        {
            return _baseDal.Update(entities);
        }
        #endregion

        #region 删除
        public bool Delete<T>(T primaryKey)
        {
            return _baseDal.Delete(primaryKey);
        }

        public bool Delete(TEntity entity)
        {
            return _baseDal.Delete(entity);
        }

        public int Delete(object[] primaryKeys)
        {
            return _baseDal.Delete(primaryKeys);
        }

        public int Delete(Expression<Func<TEntity, bool>> where)
        {
            return _baseDal.Delete(where);
        }

        #endregion


        #endregion

        #region 异步
        #region 查询

        public async Task<TEntity> QueryAsync<T>(T primaryKey, bool blnUseCache = false)
        {
            return await _baseDal.QueryAsync(primaryKey, blnUseCache).ConfigureAwait(false);
        }

        public async Task<TEntity> QueryAsync(Expression<Func<TEntity, bool>> where, bool blnUseCache = false)
        {
            return await _baseDal.QueryAsync(where, blnUseCache).ConfigureAwait(false);
        }

        public async Task<TEntity> QueryOrderAsync(Expression<Func<TEntity, bool>> expression, string strOrderByFileds = null, bool blnUseCache = false)
        {
            return await _baseDal.QueryOrderAsync(expression, strOrderByFileds, blnUseCache).ConfigureAwait(false);
        }


        public async Task<List<TEntity>> QueryListAsync(Expression<Func<TEntity, bool>> expression, int top, string strOrderByFileds = null, bool blnUseCache = false)
        {
            return await _baseDal.QueryListAsync(expression, top, strOrderByFileds, blnUseCache).ConfigureAwait(false);
        }

        public async Task<List<TEntity>> QueryListAsync(Expression<Func<TEntity, bool>> expression, string strOrderByFileds = null, bool blnUseCache = false)
        {
            return await _baseDal.QueryListAsync(expression, strOrderByFileds, blnUseCache).ConfigureAwait(false);
        }

        public async Task<PageModel<TEntity>> QueryPageAsync(Expression<Func<TEntity, bool>> where, string order, int intPageIndex = 1, int intPageSize = 20)
        {
            return await _baseDal.QueryPageAsync(where, order, intPageIndex, intPageSize).ConfigureAwait(false);
        }
        public async Task<int> QueryCountAsync(Expression<Func<TEntity, bool>> whereExpression)
        {
            return await _baseDal.QueryCountAsync(whereExpression).ConfigureAwait(false);
        }
        #endregion

        #region 添加

        public async Task<int> AddAsync(TEntity entity)
        {
            return await _baseDal.AddAsync(entity).ConfigureAwait(false);
        }

        public async Task<int> AddAsync(List<TEntity> entities)
        {
            return await _baseDal.AddAsync(entities).ConfigureAwait(false);
        }

        #endregion

        #region 修改
        public async Task<bool> UpdateAsync(TEntity entity)
        {
            return await _baseDal.UpdateAsync(entity).ConfigureAwait(false);
        }

        public async Task<bool> UpdateAsync(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> where)
        {
            return await _baseDal.UpdateAsync(columns, where).ConfigureAwait(false);
        }

        public async Task<int> UpdateAsync(List<TEntity> entities)
        {
            return await _baseDal.UpdateAsync(entities).ConfigureAwait(false);
        }
        #endregion

        #region 删除
        public async Task<bool> DeleteAsync<T>(T primaryKey)
        {
            return await _baseDal.DeleteAsync(primaryKey).ConfigureAwait(false);
        }

        public async Task<bool> DeleteAsync(TEntity entity)
        {
            return await _baseDal.DeleteAsync(entity).ConfigureAwait(false);
        }

        public async Task<int> DeleteAsync(object[] primaryKeys)
        {
            return await _baseDal.DeleteAsync(primaryKeys).ConfigureAwait(false);
        }

        public async Task<int> DeleteAsync(Expression<Func<TEntity, bool>> where)
        {
            return await _baseDal.DeleteAsync(where).ConfigureAwait(false);
        }

        #endregion
        #endregion
    }
}
